package com.generator.purchaseManage.purchaseOrderManage.controller;


import com.generator.purchaseManage.purchaseOrderManage.pojo.Brand;
import com.generator.purchaseManage.purchaseOrderManage.pojo.Product;
import com.generator.purchaseManage.purchaseOrderManage.pojo.Type;
import com.generator.purchaseManage.purchaseOrderManage.pojo.CPurchaseCon1;
import com.generator.purchaseManage.purchaseOrderManage.pojo.IncomingWarehouse;
import com.generator.purchaseManage.purchaseOrderManage.service.purchaseOrderManageService;
import com.generator.util.PageModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/purchaseOrderManage")
public class purchaseOrderManageAction {

    @Autowired
    private purchaseOrderManageService purchaseOrderManageService;

    public purchaseOrderManageService getPurchaseOrderManageService() {
        return purchaseOrderManageService;
    }

    public void setPurchaseOrderManageService(purchaseOrderManageService purchaseOrderManageService) {
        this.purchaseOrderManageService = purchaseOrderManageService;
    }

    // 查询所有采购单管理
    @RequestMapping("/getAllPurchaseOrderManage")
    public String getAllPurchaseOrderManage(Model model, CPurchaseCon1 cPurchase, PageModel<CPurchaseCon1> pageModel) {

        PageModel<CPurchaseCon1> manuManagementPage = purchaseOrderManageService.getAllPurchaseOrderManage(cPurchase, pageModel);

        List<CPurchaseCon1> data = manuManagementPage.getData();

        for (CPurchaseCon1 datum : data) {
            double totalMoney = datum.getTotalMoney();
            String s = new BigDecimal(totalMoney).toPlainString();
            datum.setTotalMoneyTemp(s);
        }

        model.addAttribute("PurchaseOrderManage", manuManagementPage);

        return "forward:/purchaseOrderManage/jumpPage";
    }

    // 跳转到purchaseStatis页面
    @RequestMapping("/jumpPage")
    public String jumpPage(Model model) {
        return "purchase/purchase/purchaseList";
    }

    // 采购单管理列表（分页）
    @RequestMapping("/purchaseOrderList")
    public String purchaseOrderList(Model model, CPurchaseCon1 cPurchase, PageModel<CPurchaseCon1> pageModel) {
        PageModel<CPurchaseCon1> purchaseOrderList = purchaseOrderManageService.getAllPurchaseOrderManage(cPurchase, pageModel);
        List<CPurchaseCon1> data = purchaseOrderList.getData();

        for (CPurchaseCon1 datum : data) {
            double totalMoney = datum.getTotalMoney();
            String s = new BigDecimal(totalMoney).toPlainString();
            datum.setTotalMoneyTemp(s);
        }

        model.addAttribute("PurchaseOrderManage", purchaseOrderList);
        return "forward:/purchaseOrderManage/jumpPage";
    }

    // 添加采购单管理列表回显数据
    @RequestMapping("/addGetDatePurchase")
    public String addGetDatePurchase(Model model, CPurchaseCon1 purchaseCon1) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        conn.setPurchaseId("CG" + new Date().getTime() + "");
        conn.setPurchaseTime(new Date());
        List<CPurchaseCon1> cPurchaseCon1s = purchaseOrderManageService.queryAllFirmNames(purchaseCon1);
        model.addAttribute("conn", conn);
        model.addAttribute("connCPurchaseCon1s", cPurchaseCon1s);
        System.out.println("cPurchaseCon1s: " + cPurchaseCon1s);
        return "purchase/purchase/purchaseAdd";
    }

    // 添加采购单管理列表
    @RequestMapping("/addPurchaseOrder")
    public String addPurchaseOrder(@Param("purchaseId") String purchaseId, @Param("totalMoney") double totalMoney,
                                   @Param("checkStatus") long checkStatus, @Param("count") int count,
                                   @Param("productIdName") String productIdName, @Param("purchasePrice") int purchasePrice,
                                   Model model) {
//                                   @Param("purchasePrice") int purchasePrice, Model model) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        // addPurchaseOrderManage 设置值
        conn.setPurchaseId(purchaseId);

        conn.setPurchaseTime(new Date());
        conn.setTotalMoney(totalMoney);
        conn.setCreatId(1);
        conn.setCheckStatus(checkStatus);
        conn.setCheckId(1);
        conn.setCheckTime(new Date());
        conn.setCheckOpinion("yyds");
        conn.setWarehuoseId(0);
        conn.setCgState(0);

        // addPurchaseOrderManage2设置值
        conn.setDetailsId(new Date().getTime());
        conn.setCount(count);
        conn.setProductModel(productIdName);

        CPurchaseCon1 productId1 = purchaseOrderManageService.getProductId(conn);

        System.out.println("productId1: " + productId1);

        conn.setProductId(productId1.getProductId());
        conn.setPurchasePrice(purchasePrice);

        int i = purchaseOrderManageService.addPurchaseOrderManage(conn);
        int j = purchaseOrderManageService.addPurchaseOrderManage2(conn);

        if (i > 0 && j > 0) {
            return "forward:/purchaseOrderManage/getAllPurchaseOrderManage";
        } else {
            return "forward:/purchaseOrderManage/addPurchaseOrder";
        }
    }

    // 查询单个采购单管理信息
    @RequestMapping("/getOnePurchaseOrderManage/{id}")
    public String getOnePurchaseOrderManage(@PathVariable("id") String id, Model model) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        conn.setPurchaseId(id);

        CPurchaseCon1 oneManuManagement = purchaseOrderManageService.getOnePurchaseOrderManage(conn);
        CPurchaseCon1 oneManuManagementDown = purchaseOrderManageService.getOnePurchaseOrderManageDown(conn);

        System.out.println("oneManuManagement: " + oneManuManagement);
        System.out.println("oneManuManagementDown: " + oneManuManagementDown);

        model.addAttribute("cDetailedPurchases", oneManuManagement);
        model.addAttribute("cDetailedPurchasesDown", oneManuManagementDown);
        return "purchase/purchase/purchaseView";

    }

    // 删除单个采购单管理信息
    @RequestMapping("/deletePurchaseOrderManage/{id}")
    public String deletePurchaseOrderManage(@PathVariable("id") String id) {
        int i = purchaseOrderManageService.deletePurchaseOrderManage(id);
        return "forward:/purchaseOrderManage/getAllPurchaseOrderManage";
    }

    // 采购单分类查询
    @RequestMapping("/vaguePurchaseOrderManage")
    public String vaguePurchaseOrderManage(@Param("purchaseId") String purchaseId, @Param("date1") String date1,
                                           @Param("date2") String date2, @Param("totalMoney1") String totalMoney1,
                                           @Param("totalMoney2") String totalMoney2, @Param("checkStatus") long checkStatus,
                                           Model model, PageModel<CPurchaseCon1> pageModel) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        double totalMoney1double = -1;
        double totalMoney2double = -1;

        if (Objects.equals(purchaseId, "%") || Objects.equals(purchaseId, "_")){
            conn.setPurchaseId(")(@#~【】");
        }else {
            conn.setPurchaseId(purchaseId);
        }

        System.out.println("purchaseId: " + purchaseId);

        if (Objects.equals(totalMoney1, "") || totalMoney1 == null) {
            conn.setTotalMoney1(-1);
        }else{
            totalMoney1double = Double.parseDouble(totalMoney1);
        }

        if (Objects.equals(totalMoney2, "") || totalMoney2 == null) {
            conn.setTotalMoney2(-1);
        }else{
            totalMoney2double = Double.parseDouble(totalMoney2);
        }

        double temp;

        if (totalMoney1double > totalMoney2double) {
            temp = totalMoney1double;
            totalMoney1double = totalMoney2double;
            totalMoney2double = temp;
        }

        conn.setTotalMoney1(totalMoney1double);
        conn.setTotalMoney2(totalMoney2double);

        if (Objects.equals(date1, "") || date1 == null) {
            conn.setData1("");
        } else {
            conn.setData1(date1);
        }

        if (Objects.equals(date2, "") || date2 == null) {
            conn.setData2("");
        } else {
            conn.setData2(date2);
        }

        if (checkStatus != 0) {
            conn.setCheckStatus(checkStatus);
        }

        PageModel<CPurchaseCon1> purchasePage = purchaseOrderManageService.vaguePurchaseOrderManage(conn, pageModel);

        List<CPurchaseCon1> data = purchasePage.getData();

        for (CPurchaseCon1 datum : data) {
            double totalMoney = datum.getTotalMoney();
            String s = new BigDecimal(totalMoney).toPlainString();
            datum.setTotalMoneyTemp(s);
        }

        model.addAttribute("PurchaseOrderManage", purchasePage);
        return "forward:/purchaseOrderManage/jumpPage";
    }

    // 修改采购单管理列表回显数据
    @RequestMapping("/updateGetDatePurchase/{id}")
    public String updateGetDatePurchase(@PathVariable("id") String id, Model model) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        conn.setPurchaseId(id);

        // 回显数据
        CPurchaseCon1 oneManuManagementDown = purchaseOrderManageService.updateGetDatePurchase(conn);

        // 查询所有品牌
        List<CPurchaseCon1> cPurchaseCon1s = purchaseOrderManageService.queryAllBrands(conn);

        // 查询所有品牌类型
        List<CPurchaseCon1> cPurchaseCon2s = purchaseOrderManageService.queryAllTypes(conn);

        // 查询所有品牌型号
        List<CPurchaseCon1> cPurchaseCon3s = purchaseOrderManageService.queryAllModels(conn);

        // 查询所有厂商
        List<CPurchaseCon1> cPurchaseCon4s = purchaseOrderManageService.queryAllFirms(conn);

        model.addAttribute("conn", oneManuManagementDown);
        model.addAttribute("conn1", cPurchaseCon1s);
        model.addAttribute("conn2", cPurchaseCon2s);
        model.addAttribute("conn3", cPurchaseCon3s);
        model.addAttribute("conn4", cPurchaseCon4s);

        return "purchase/purchase/purchaseUpdate";
    }

    // 修改采购单管理列表数据
    @RequestMapping("/updatePurchaseOrder")
    public String updatePurchaseOrder(@Param("purchaseId") String purchaseId, @Param("count") int count, @Param("detailsId") String detailsId,
                                      @Param("purchasePrice") int purchasePrice, @Param("purchaseId") double totalMoney) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        conn.setPurchaseId(purchaseId);
        conn.setCount(count);
        conn.setPurchasePrice(purchasePrice);
        conn.setTotalMoney(totalMoney);
        long detailsIdTemp = Long.parseLong(detailsId);
        conn.setDetailsId(detailsIdTemp);

        int i = purchaseOrderManageService.updatePurchaseOrder(conn);
        int j = purchaseOrderManageService.updatePurchaseOrder2(conn);

        if (i > 0 && j > 0) {
            return "forward:/purchaseOrderManage/getAllPurchaseOrderManage";
        } else {
            return null;
        }
    }

    // 采购单入库详情 purchaseWarehousingDetails
    @RequestMapping("/purchaseWarehousingDetails/{purchaseId}")
    public String purchaseWarehousingDetails(@PathVariable("purchaseId") String purchaseId, Model model,  CPurchaseCon1 cPurchase, PageModel<CPurchaseCon1> pageModel) {

        CPurchaseCon1 conn = new CPurchaseCon1();

        if (purchaseId != null && !purchaseId.equals("")){
            conn.setPurchaseId(purchaseId);
        }

        CPurchaseCon1 oneManuManagementDown = purchaseOrderManageService.purchaseWarehousingDetails(conn);
        model.addAttribute("conn", oneManuManagementDown);

        CPurchaseCon1 oneManuManagementDown1 = purchaseOrderManageService.getOnePurchaseOrderManageDown(conn);
        model.addAttribute("cDetailedPurchasesDown", oneManuManagementDown1);

        if (oneManuManagementDown == null || oneManuManagementDown1 == null){
            model.addAttribute("cDetailedPurchasesFlag", 1);

            PageModel<CPurchaseCon1> manuManagementPage = purchaseOrderManageService.getAllPurchaseOrderManage(cPurchase, pageModel);

            List<CPurchaseCon1> data = manuManagementPage.getData();

            for (CPurchaseCon1 datum : data) {
                double totalMoney = datum.getTotalMoney();
                String s = new BigDecimal(totalMoney).toPlainString();
                datum.setTotalMoneyTemp(s);
            }

            model.addAttribute("PurchaseOrderManage", manuManagementPage);
            return "forward:/purchaseOrderManage/jumpPage";
        }else {
            model.addAttribute("cDetailedPurchasesFlag", 2);
            return "storage/stock/stockView";
        }

    }

    // 查看入货仓库
    @RequestMapping("/getIncomingWarehouse/{id}")
    public String getIncomingWarehouse(@PathVariable("id") int id, Model model) {

        IncomingWarehouse conn = new IncomingWarehouse();
        conn.setId(id);
        IncomingWarehouse oneManuManagementDown = purchaseOrderManageService.getIncomingWarehouse(conn);
        model.addAttribute("conn", oneManuManagementDown);
        System.out.println("id: " + id);
        System.out.println("getIncomingWarehouse: " + oneManuManagementDown);

        return "storage/storage/storageView";
    }

    // 发送审核请求，将审核状态改成审核中
    @RequestMapping("/sendApproval")
    public String sendApproval(@RequestParam("namePro") String namePro, Model model, PageModel<CPurchaseCon1> pageModel) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        conn.setPurchaseId(namePro);

        CPurchaseCon1 cPurchase1 = new CPurchaseCon1();

        int i = purchaseOrderManageService.sendApproval(conn);
        PageModel<CPurchaseCon1> manuManagementPage = purchaseOrderManageService.getAllPurchaseOrderManage(cPurchase1, pageModel);

        model.addAttribute("PurchaseOrderManage", manuManagementPage);

        if (i > 0) {
            return "forward:/purchaseOrderManage/jumpPage";
        } else {
            return null;
        }

    }

    // 查询全部未审核
    @RequestMapping("/queryAllUnapproved")
    public String queryAllUnapproved(Model model, CPurchaseCon1 cPurchase, PageModel<CPurchaseCon1> pageModel) {

        PageModel<CPurchaseCon1> queryAllUnapproved = purchaseOrderManageService.queryAllUnapproved(cPurchase, pageModel);
        List<CPurchaseCon1> data = queryAllUnapproved.getData();
        for (CPurchaseCon1 datum : data) {
            double totalMoney = datum.getTotalMoney();
            String s = new BigDecimal(totalMoney).toPlainString();
            datum.setTotalMoneyTemp(s);
        }

        model.addAttribute("queryAllUnapproved", queryAllUnapproved);

        return "forward:/purchaseOrderManage/jumpPageUnapproved";
    }

    // 跳转到 purchaseExamineList 页面
    @RequestMapping("/jumpPageUnapproved")
    public String jumpPageUnapproved(Model model) {
        return "purchase/purchaseExamine/purchaseExamineList";
    }

    // 查询全部未审核（分页）
    @RequestMapping("/queryAllPageUnapproved")
    public String queryAllPageUnapproved(Model model, CPurchaseCon1 cPurchaseCon1, PageModel<CPurchaseCon1> pageModel) {
        PageModel<CPurchaseCon1> queryAllPageUnapproved = purchaseOrderManageService.queryAllUnapproved(cPurchaseCon1, pageModel);
        PageModel<CPurchaseCon1> queryAllUnapproved = purchaseOrderManageService.queryAllUnapproved(cPurchaseCon1, pageModel);

        List<CPurchaseCon1> data = queryAllUnapproved.getData();
        for (CPurchaseCon1 datum : data) {
            double totalMoney = datum.getTotalMoney();
            String s = new BigDecimal(totalMoney).toPlainString();
            datum.setTotalMoneyTemp(s);
        }

        model.addAttribute("queryAllUnapproved", queryAllUnapproved);
        model.addAttribute("queryAllPageUnapproved", queryAllPageUnapproved);
        return "forward:/purchaseOrderManage/jumpPageUnapproved";
    }

    // 查询全部未审核详情信息
    @RequestMapping("/queryOneUnapproved/{id}")
    public String queryOneUnapproved(@PathVariable("id") String id, Model model) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        conn.setPurchaseId(id);

        CPurchaseCon1 oneOneUnapproved = purchaseOrderManageService.queryOneUnapproved(conn);
        model.addAttribute("oneOneUnapproved", oneOneUnapproved);

        // 查询全部未审核详情信息下面部分
        CPurchaseCon1 oneOneUnapprovedDown = purchaseOrderManageService.queryOneUnapprovedDown(conn);
        model.addAttribute("oneOneUnapprovedDown", oneOneUnapprovedDown);

        return "purchase/purchaseExamine/purchaseExamine";

    }

    // 采购单审核提交
    @RequestMapping("/purchaseOrderReviewAndSubmission")
    public String purchaseOrderReviewAndSubmission(@Param("textinput") String textinput, @Param("dfselect") int dfselect,
                                                   @Param("purchaseId") String purchaseId, Model model, PageModel<CPurchaseCon1> pageModel) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        if (purchaseId != null && purchaseId != "") {
            conn.setPurchaseId(purchaseId);
        }

        if (Objects.equals(textinput, "") || textinput == null) {
            conn.setTextinput("");
        } else {
            conn.setTextinput(textinput);
        }

        conn.setCheckStatus(dfselect);

        int i = purchaseOrderManageService.purchaseOrderReviewAndSubmission(conn);

        if (i > 0) {
            return "forward:/purchaseOrderManage/queryAllUnapproved";
        } else {
            return null;
        }
    }

    // 采购单审核分类查询 vaguePurchaseOrderReview
    @RequestMapping("/vaguePurchaseOrderReview")
    public String vaguePurchaseOrderReview(@Param("purchaseId") String purchaseId, @Param("date1") String date1,
                                           @Param("date2") String date2, @Param("totalMoney1") String totalMoney1,
                                           @Param("totalMoney2") String totalMoney2,
                                           Model model, PageModel<CPurchaseCon1> pageModel) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        conn.setPurchaseId(purchaseId);

        if (Objects.equals(purchaseId, "%") || Objects.equals(purchaseId, "_")){
            conn.setPurchaseId(")(@#~【】");
        }else {
            conn.setPurchaseId(purchaseId);
        }

        if (Objects.equals(totalMoney1, "") || totalMoney1 == null) {
            conn.setTotalMoney1(-1);
        } else {
            conn.setTotalMoney1(Double.parseDouble(totalMoney1));
        }

        if (Objects.equals(totalMoney2, "") || totalMoney2 == null) {
            conn.setTotalMoney2(-1);
        } else {
            conn.setTotalMoney2(Double.parseDouble(totalMoney2));
        }

        if (Objects.equals(date1, "") || date1 == null) {
            conn.setData1("");
        } else {
            conn.setData1(date1);
        }

        if (Objects.equals(date2, "") || date2 == null) {
            conn.setData2("");
        } else {
            conn.setData2(date2);
        }
        PageModel<CPurchaseCon1> purchasePage = purchaseOrderManageService.vaguePurchaseOrderReview(conn, pageModel);

        List<CPurchaseCon1> data = purchasePage.getData();

        for (CPurchaseCon1 datum : data) {
            double totalMoney = datum.getTotalMoney();
            String s = new BigDecimal(totalMoney).toPlainString();
            datum.setTotalMoneyTemp(s);
        }

        model.addAttribute("queryAllUnapproved", purchasePage);
        return "forward:/purchaseOrderManage/jumpPageUnapproved";
    }

    //添加，修改页面加载完成查询所有品牌
    @RequestMapping("/queryBrand")
    @ResponseBody
    public List<Brand> queryBrand(){
        return  purchaseOrderManageService.queryBrand();
    }

    //根据品牌查询类型
    @RequestMapping("/queryTypeByBrand")
    @ResponseBody
    public List<Type> queryTypeByBrand(Brand brand, HttpServletRequest request){
        String brandId = request.getParameter("brandId");
        if (brandId!=null&&brandId!=""){
            int i = Integer.parseInt(brandId);
            brand.setBrandId(i);
            return  purchaseOrderManageService.queryTypeByBrand(brand);
        }
        return null;
    }

    //根据类型查询型号
    @RequestMapping("/queryModelByType")
    @ResponseBody
    public List<Product> queryModelByType(Type type, HttpServletRequest request){
        String brandId = request.getParameter("brandId");
        String typeId = request.getParameter("typeId");
        if (typeId!=null&&typeId!=""){
            int i1 = Integer.parseInt(typeId);
            type.setTypeId(i1);
            return  purchaseOrderManageService.queryModelByType(type);
        }
        return null;
    }

    //根据商品id查商品信息
    @RequestMapping("/queryProductDetail")
    @ResponseBody
    public Product queryProductDetail(Product product, HttpServletRequest request, Model model){
        String productId = request.getParameter("productId");
        if (productId!=null&&productId!=""){
            int i = Integer.parseInt(productId);
            product.setProductId(i);
            Product product1 = purchaseOrderManageService.queryProductDetail(product);
            model.addAttribute("product1", product1);
            return product1;
        }
        return null;
    }

}
